package eu.base.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class BufferedLineIterator implements TextLineIterator {

	String encoding = "UTF-8";
	List<String> buffer = null;
	ListIterator<String> it = null;

	public BufferedLineIterator(File file, String encoding) {
		try {
			this.encoding = encoding;
			buffer = new LinkedList<String>();
			TextOperator tr = new TextOperator(file);
			buffer.addAll(tr.readLines());
			it = buffer.listIterator();
			tr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public BufferedLineIterator(List<String> lines) {
		buffer = lines;
		it = buffer.listIterator();
	}

	@Override
	public boolean hasNext() {
		return it.hasNext();
	}

	@Override
	public String next() {
			return it.next();
	}

	@Override
	public void remove() {
			it.remove();
	}

	public boolean usingBuffer() {
		return buffer != null;
	}

	public void close() {
	}

	@Override
	public boolean hasPrevious() {
		return it.hasPrevious();
	}

	@Override
	public String previous() {
		return it.previous();
	}

	@Override
	public int nextIndex() {
		return it.nextIndex();
	}

	@Override
	public int previousIndex() {
		return it.previousIndex();
	}

	@Override
	public void set(String e) {
		it.set(e);
	}

	@Override
	public void add(String e) {
		it.add(e);
	}
}
